﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.ComponentModel;
using Microsoft.VisualBasic.Activities;
using TransactionServer.General_Classes;


namespace TransactionServer.CRM4Activities
{
    [Designer(typeof(TransactionServer.ActivityDesign.CRMActivities.CRMGetEntityById))]
    public sealed class CRM4GetEntityById : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<Context> Context { get; set; }
        public InArgument<string[]> Attributes { get; set; }
        public InArgument<String> EntityName { get; set; }
        public InArgument<String> EntityId { get; set; }
        public InArgument<String> SystemName { get; set; }

        public OutArgument<Entity> Target { get; set; }

        public CRM4GetEntityById()
        {
            VisualBasicValue<Context> vbArgument = new VisualBasicValue<Context>(Constants.CONTEXT_NAME);
            this.Context = InArgument<Context>.FromExpression(vbArgument);
        }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string[] attributes = context.GetValue(this.Attributes);
            string entityName = context.GetValue(this.EntityName);
            string entityId = context.GetValue(this.EntityId);
            string systemName = context.GetValue(this.SystemName);
            var con = context.GetValue(this.Context);
            CRM4Connector conn = (CRM4Connector)con.Connector[systemName];

            context.SetValue(this.Target, conn.getEntityById(systemName, entityName, entityId, attributes));
        }
    }
}
